package cn.jupiter.json;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * parse client's json request to database resultset.
 * @author pizhenhua@gmail.com
 * example "column1":"value1","column2":"value2","column3":"value3"}
 */
public class JsonResultSet {
	private String json;
	private Map<String,Object> rows=new HashMap<String,Object>();
	/**
	 * parse
	 */
	public void parse(){
		//if start with {[ s a array
		if(json.startsWith("{[")){
			
		}
		//if start with { a single row
		if(json.startsWith("{\"")){
			Pattern p = Pattern.compile("(?:\"\\w+\"):(?:\"[^\"]+\")");            
			Matcher m = p.matcher(json);
			String[] _strs = null;
			while (m.find()) {            
			_strs = m.group().split(":");            
			if(_strs.length == 2 )
				rows.put(_strs[0].replaceAll("'|\"", ""), _strs[1].trim().replaceAll("'|\"", ""));
			}
		}
		

	}
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}
	public Map<String, Object> getRows() {
		return rows;
	}
	public void setRows(Map<String, Object> rows) {
		this.rows = rows;
	}
	
}
